﻿
using System;

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;

public partial class sysadmin_DL_Project_AddStudYear : PowerCreator.LMS.Web.Base.WebBasePage
{
    TOXuenian obj = new TOXuenian();
    private PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
    private string url = "../../login.aspx";

    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUser();
        if (!IsPostBack)
        {
            InitPage();            
            int SID = 0; 
            if (Request.QueryString["SID"] != null&&Request.QueryString["Action"] != null)
            {
                SID = int.Parse(Request.QueryString["SID"].ToString());
                ViewState["SID"] = SID.ToString();
                ViewState["Action"] = Request.QueryString["Action"].ToString();
                if (ViewState["Action"].ToString() == "edit")
                    BinderForm(SID);
            }

        }
    }
    private void CheckUser()
    {
        if (Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO] == null)
        {
            Script.RedirectNewUrl(url);
        }
        else
        {
            login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
            if (login == null)
            {
                Script.RedirectNewUrl(url);
            }
            else
            {
                if (login.VOAdvancedUser == null)
                {
                    Script.RedirectNewUrl(url);
                }
                else if (login.VOAdvancedUser.AUserGroup > 2)
                {
                    Script.ShowAndHistory("您无此权限！", -1);
                }
            }

        }

    }

    protected override void InitPage()
    {
        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    private void BinderForm(int id)
    {
        VOTOXueNian vo = new VOTOXueNian();
        vo = obj.Load(id);

        txt_School.Text = vo.XueNian;
    }
    private bool CheckPageData()
    {
        string search = string.Format("XueNian = '{0}'", txt_School.Text.Trim());
        if (ViewState["SID"] != null)
        {
            search += "and ID<>" + ViewState["SID"].ToString();
        }
        if (obj.Count(search) != 0)
        {
            lbl_msg_Error.Text = "<br /> 该学年名已存在！" ;
            return false;
        }
        return true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!CheckPageData()) return;
        VOTOXueNian vo = new VOTOXueNian();
        PowerCreator.LMS.Web.Base.BasePage bpage = new PowerCreator.LMS.Web.Base.BasePage();
        if (ViewState["Action"].ToString()=="add")
        {
            vo.XueNian= txt_School.Text.Trim();            
            vo.Creator = bpage.GetCreatedByInfo().UserID;
            vo.Createtime = DateTime.Now;
            
            if (obj.Add(vo))
            {
                string url = "ManageStudYear.aspx";
                Script.RedirectURL(url);
            }
        }
        else if(ViewState["Action"].ToString()=="edit")
        {
            vo = obj.Load(int.Parse(ViewState["SID"].ToString()));
            vo.XueNian = txt_School.Text.Trim();
            vo.Creator = bpage.GetCreatedByInfo().UserID;
            vo.Createtime = DateTime.Now;
            
            if (obj.Update(vo))
            {
                string url = "ManageStudYear.aspx";
                Script.RedirectURL(url);
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "ManageStudYear.aspx";
        Script.RedirectURL(url);
    }

}
